package com.sl.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.sl.entity.PageResult;
import com.sl.entity.Result;
import com.sl.entity.StatusCode;
import com.sl.pojo.Company;
import com.sl.pojo.Option;
import com.sl.pojo.Question;
import com.sl.pojo.QuestionOption;
import com.sl.service.OptionService;
import com.sl.service.QuestionOptionService;
import com.sl.service.QuestionService;
import com.sl.vo.QuestionVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Slf4j
@RestController
@RequestMapping("/question")
public class QuestionController  {
    @Autowired
    private QuestionService questionService;
    @Autowired
    private QuestionOptionService questionOptionService;
    @Autowired
    private OptionService optionService;

    //所有的问题
    @PostMapping("/all")
    public Result allQuesitons(@RequestBody String data){
        log.info("查看题库所有的题");
        if(StringUtils.isEmpty(data)){data="{\"page\":1,\"pageSize\":10}";}
        JSONObject jsonObject = JSONObject.parseObject(data);
        Integer page = jsonObject.getInteger("page");
        if (null ==page) {page=1;}
        Integer pageSize = jsonObject.getInteger("pageSize");
        if (null ==pageSize) {pageSize = 10;}
        Page<Question> all = questionService.findAllQuestions(page, pageSize);
        List<QuestionVO> voList=new ArrayList<>();
        for (Question question : all) {
            QuestionVO questionVO = question2VO(question);
            voList.add(questionVO);
        }
        PageResult pageResult=new PageResult((long)voList.size(),voList);
        return new Result<Question>(true, StatusCode.OK,"显示题库",pageResult);
    }
   //显示问题和选项
    public  QuestionVO question2VO(Question question){
        log.info("question2VO>>>" + question);
        Integer questionId = question.getId();
        String content = question.getContent();
        String type = question.getType();

        QuestionVO questionVO = new QuestionVO();
        questionVO.setQuestionId(questionId);
        questionVO.setContentVO(content);

          String options="";
            Set<Integer> oidByQids = questionOptionService.findOidByQid(questionId);
            if(null !=oidByQids){
                if(type.equals("choose")){
                        for (Integer oId : oidByQids) {
                            Option option = optionService.findById(oId);
                            String questionOption = option.getQuestionOption();
                            options  +=questionOption + "/";
                    }
                    String optionsSubstring = options.substring(0, options.length() - 1);
                    questionVO.setOption(optionsSubstring);
                }
                else  if(type.equals("short")){
                    questionVO.setOption(options);
                }
                else {
                    questionVO.setOption("请确定题目的类型:choose或short或新增");
                }
            }else {
                questionVO.setOption("请确定题目的类型:choose或short或新增");
            }
            log.info("questionVO" + questionVO);

        return questionVO;
    }

    @PostMapping("/adds")
    public Result addQuestion(@RequestBody String data){
        log.info("增加选题");
        if(StringUtils.isEmpty(data)){ return  new Result(false, StatusCode.ERROR,"参数为空");}
        List<Question> questions = JSON.parseArray(data, Question.class);
        for (Question question : questions) {
            log.info("增加多个问题>>>"+question);
            questionService.addQuestion(question);
        }
        return new Result(true,StatusCode.OK,"新增题库信息成功");
    }

    //给问题question选择答案option。
    @PostMapping("/question2option")
    public Result question2Option(@RequestBody String data){
        log.info("给问题question选择复用答案option");
        if(StringUtils.isEmpty(data)){ return  new Result(false, StatusCode.ERROR,"参数为空");}
        JSONObject jsonObject = JSON.parseObject(data);
        Integer qId = jsonObject.getInteger("questionId");
        if(null == qId  ){ return  new Result(false, StatusCode.ERROR,"questionId参数为空");}
        Question byId = questionService.findById(qId);
        if(null == byId ){ return  new Result(false, StatusCode.ERROR,"没有这到题目，请确认QuestionId");}
        String options = jsonObject.getString("options");
        System.out.println(options);
        if(StringUtils.isEmpty(options)){ return  new Result(false, StatusCode.ERROR,"options参数为空");}
        String[] stringSplit =  options.split("/");
            for (String str : stringSplit) {
                Option qOption = optionService.findByQuestionOption(str);
                //如果。答案项。没有questionOption。增加。并且关联
                if (null == qOption){
                    Option option = new Option();
                    option.setQuestionOption(str);
                    log.info("答案不存在，新增Option答案:"+ str );
                    optionService.add(option);//增加答案项目
                    Option newOption= optionService.findByQuestionOption(str);
                    //q-o关联
                    QuestionOption questionOption = new QuestionOption();
                    questionOption.setQId(qId);
                    questionOption.setOId(newOption.getId());
                    //关联
                    questionOptionService.addQuestionOption(questionOption);
                    log.info("新增Question--Option关联:" + questionOption);
                }
                if(null != qOption){
                    //不为空：有一个值存在。
                //如果Option中存在。关联，加入QuestionOption中间表
                QuestionOption questionOption = new QuestionOption();
                questionOption.setOId(qOption.getId());
                questionOption.setQId(qId);
                log.info("答案存在，Question--Option关联:" + questionOption);
                questionOptionService.addQuestionOption(questionOption);
                }
                System.out.println(qOption);
            }

        return new Result(true,StatusCode.OK,"问题question选择复用答案成功");
    }

    @DeleteMapping("/deleted")
    public  Result deletePaper(@RequestBody String data ){
        log.info("删除试题" + data);
        Question question = JSON.parseObject(data, Question.class);
        Integer questionId = question.getId();
        List<QuestionOption> byQid = questionOptionService.findByQid(questionId);
        if(byQid.size() > 0){
            for (QuestionOption questionOption : byQid) {
                log.info("删除QuestionOption关联");
                questionOptionService.deleted(questionOption);
            }
        }
        System.out.println("question>>" + question);
        questionService.deleted(question);
        return new Result<Question>(true, StatusCode.OK,"删除试题成功");
    }
}
